﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FriendTest.FriendService;
using FriendTest.Models;
using FriendTest.Media;

namespace FriendTest.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private Service1 myService = new Service1();

        private MediaService myMediaService = new MediaService();

        private Dictionary<int, string> resourceTypeDict = new Dictionary<int, string>();

        public HomeController()
        {
            resourceTypeDict.Add(1, "图书"+",Book");
            resourceTypeDict.Add(2, "电影"+",Movie");
            resourceTypeDict.Add(3, "音乐专辑"+",Music");
        }

        private void SetNewsComment(News news, List<MyComment> comments)
        {
            FriendService.Comment[] allComment = myService.GetCommentList(news.newsPublishItemId);

            foreach (FriendService.Comment comment in allComment)
            {
                MyComment newComment = new MyComment
                {
                    commentId = comment.id,
                    commentContent = comment.commentContent,
                    commentDate = comment.commentDate,
                    commentFrom = myService.GetUserData(comment.fromUserId)
                };
                comments.Add(newComment);
            }
        }

        [Authorize]
        public ActionResult Index()
        {
            int id = int.Parse(HttpContext.User.Identity.Name.Split(',')[1]);
            UserData curUser = myService.GetUserData(id);
            return View(curUser);
        }

        public ActionResult About()
        {
            return View();
        }

        [Authorize]
        public ActionResult Profile(int ID) {
            UserData User = myService.GetUserData(ID);
            int currentUser = int.Parse(HttpContext.User.Identity.Name.Split(',')[1]);
            ViewData["IsFriend"] = myService.IsFriend(currentUser, ID);
            return View(User);
        }

        public ActionResult ShowCmtList(int id)
        {
            List<MyComment> cmtList = new List<MyComment>();
            FriendService.Comment[] allCmt = myService.GetCommentList(id);
            foreach (var comment in allCmt)
            {
                cmtList.Add(new MyComment
                {
                    commentId = comment.id,
                    commentContent = comment.commentContent,
                    commentDate = comment.commentDate,
                    commentFrom = myService.GetUserData(comment.fromUserId)
                });
            }
            return View(cmtList);
        }

        public ActionResult AddComment(int itemId, int fromUser, int toUser, string comment)
        {
            //myService.AddCommentToItem(id,);
            myService.AddCommentToItem(itemId, comment, fromUser, toUser);
            //return "comment";
            //return "<li>itemId:" + itemId + "--from:" + fromUser + "--to:" + toUser + "--content:" + comment + "</li>";
        //    return "<%Html.RenderAction(\"ShowCmtList\", new { id = news.publishItemId}); %>";
            return RedirectToAction("ShowCmtList", new { id=itemId});
        }

        public ActionResult ShowStatusByItemId(int itemId)
        {
            PublishStatus selectedStatus = myService.GetStatusByPublishId(itemId);
            MyPublishStatus myStatus = new MyPublishStatus
            {
                itemType = 0,
                itemId = selectedStatus.id,
                itemOwner = myService.GetUserData(selectedStatus.userId),
                itemDate = selectedStatus.publishDate,
                statusContent = selectedStatus.statusContent
            };
            return View(myStatus);
        }

        public ActionResult ShowShareByItemId(int itemId)
        {
            PublishShare selectedShare = myService.GetShareByItemId(itemId);

            int resourceId = selectedShare.resourceId;

            int type = myMediaService.GetResourceTableIndex(resourceId);

            MyPublishShare share = new MyPublishShare
            {
                itemType = 1,
                itemId = selectedShare.id,
                itemOwner = myService.GetUserData(selectedShare.userId),
                itemDate = selectedShare.publishDate,
                resourceId = selectedShare.resourceId,
                resourceType = resourceTypeDict[type]
            };

            EntityObject resource = myMediaService.GetResourceDetail(resourceId);
            switch (type)
            {
                case 1: share.resourceName = ((BookInfo)resource).title;
                        share.resourceImg = ((BookInfo)resource).imageURL;
                        break;
                case 2: share.resourceName = ((MovieInfo)resource).title;
                        share.resourceImg = ((MovieInfo)resource).imageURL;
                        break;
                case 3: share.resourceName = ((MusicInfo)resource).title; 
                        share.resourceImg = ((MusicInfo)resource).imageURL;
                        break;
                default: break;
            }
            return View(share);
        }
      
        public ActionResult ShowNewsList(int userId)
        {
            News[] newsList = myService.GetNewsList(userId);
            List<MyNews> allNews = new List<MyNews>();

            foreach (var news in newsList)
            {
                PublishItem selectedItem = myService.GetPublishItemByPublishId(news.newsPublishItemId);

                MyPublishItem myPublish = new MyPublishItem
                {
                    itemId = selectedItem.id,
                    itemDate = selectedItem.publishDate,
                    itemOwner = myService.GetUserData(selectedItem.userId),
                    itemType = selectedItem.type
                };
                if (selectedItem != null)
                {
                    MyNews newNews = new MyNews
                    {
                        newsId = news.id,
                        userId = news.newsOwnerId,
                        newsFrom = myService.GetUserData(selectedItem.userId),
                        newsPublishItem = myPublish,
                    };
                    if(myService.IsFriend(userId,selectedItem.userId))
                        allNews.Add(newNews);
                }
            }
            return View(allNews);
        }

        public ActionResult ShowUserStatusByID(int userId)
        {
            PublishStatus[] StatusList = myService.GetStatusByUserId(userId);
            List<PublishStatus> allStatus = new List<PublishStatus>();

            foreach (var news in StatusList)
            {
                allStatus.Add(news);
            }
            return View(allStatus);

        }

        public ActionResult DeleteNews(int newsId, int user)
        {
            myService.DeleteNewsById(newsId);
            return RedirectToAction("ShowNewsList",new{userId = user});
        }

        public string PublishStatus(int id, string publish_content)
        {
            if (myService.PublishAStatus(id, publish_content))
                return "发布成功";
            else
                return "发布失败";
        }

        public string AddFriend(int userId)
        {
            int id = int.Parse(HttpContext.User.Identity.Name.Split(',')[1]);
            if (myService.AddFriend(id,userId).Contains("successfully"))
                return "关注成功";
            else
                return "关注失败";
        }

        public string DeleteFriend(int userId)
        {
            int id = int.Parse(HttpContext.User.Identity.Name.Split(',')[1]);
            if (myService.DeleteFriend(id, userId))
            {
                return "取消关注成功";
            }
            else
            {
                return "取消关注失败";
            }
            //return "";
        }
     }
}
